<template>
  <div class="add-enterprise">
    <header class="add-header">
      <div class="left">
        <span class="arrow" @click="$router.back()"><i class="el-icon-arrow-left" />返回</span>
        <span style="margin-left: 20px;">|</span>
        <span style="margin-left: 20px;">{{ title }}</span>
      </div>
      <div class="right">
        黑马程序员
      </div>
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">企业信息</div>
        <div class="form">
          <el-form ref="ruleForm" label-width="100px" :model="addForm" :rules="ruleForm">
            <el-form-item label="企业名称" prop="name">
              <el-input v-model="addForm.name" placeholder="请输入企业名称" />
            </el-form-item>
            <el-form-item label="法人" prop="legalPerson">
              <el-input v-model="addForm.legalPerson" placeholder="请输入法人" />
            </el-form-item>
            <el-form-item label="注册地址" prop="registeredAddress">
              <el-input v-model="addForm.registeredAddress" placeholder="请输入注册地址" />
            </el-form-item>
            <el-form-item label="所在行业" prop="industryCode">
              <el-select v-model="addForm.industryCode">
                <el-option v-for="item in industryList" :key="item.industryCode" :label="item.industryName" :value="item.industryCode" />
              </el-select>
            </el-form-item>
            <el-form-item label="营业执照" prop="businessLicenseUrl">
              <el-upload
                action="#"
                :http-request="uploadRequest"
                :before-upload="beforerUpload"
              >
                <el-button size="small"><i class="el-icon-upload2" />点击上传</el-button>
                <div slot="tip" class="el-upload__tip">支持扩展名：.png .jpg .jpeg，文件大小不得超过5M</div>
              </el-upload>

            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="form-container">
        <div class="title">联系人信息</div>
        <div class="form">
          <el-form ref="ruleForm" label-width="100px" :model="addForm" :rules="ruleForm">
            <el-form-item label="企业联系人" prop="contact">
              <el-input v-model="addForm.contact" placeholder="请输入企业联系人" />
            </el-form-item>
            <el-form-item label="联系电话" prop="contactNumber">
              <el-input v-model="addForm.contactNumber" placeholder="请输入联系电话" />
            </el-form-item>
          </el-form>
        </div>
      </div>
    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button @click="resetBtn">重置</el-button>
        <el-button type="primary" @click="submitBtn">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { industryList, uploadLicense, addEnterprise, getEnterpriseDetail, updateEnterprise } from '@/api/Enterprise'
export default {
  data() {
    return {
      addForm: {
        name: '', // 企业名称
        legalPerson: '', // 法人
        registeredAddress: '', // 注册地址
        industryCode: '', // 所在行业
        contact: '', // 企业联系人
        contactNumber: '', // 联系人电话
        businessLicenseUrl: '', // 营业执照url
        businessLicenseId: '' // 营业执照id
      },
      params: {
        file: '',
        type: ''
      },
      industryList: [],
      ruleForm: {
        name: [{ required: true, message: '请输入企业名称', trigger: 'blur' }],
        legalPerson: [{ required: true, message: '请输入法人', trigger: 'blur' }],
        registeredAddress: [{ required: true, message: '请输入注册地址', trigger: 'blur' }],
        industryCode: [{ required: true, message: '请输入所在行业', trigger: 'blur' }],
        contact: [{ required: true, message: '请输入企业联系人', trigger: 'blur' }],
        contactNumber: [
          { required: true, message: '请输入联系电话', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        businessLicenseUrl: [{ required: true, message: '必填项，内容不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      if (this.$route.params.id) {
        return '编辑企业'
      } else {
        return '添加企业'
      }
    }
  },
  created() {
    if (this.$route.params.id) {
      this.edit()
    }
  },

  mounted() {
    this.getIndustryList()
  },
  methods: {
    async edit() {
      const res = await getEnterpriseDetail(this.$route.params.id)
      this.addForm = res.data
    },
    async getIndustryList() {
      const { data } = await industryList()
      this.industryList = data
    },
    async uploadRequest(data) {
      const file = data.file
      const formData = new FormData()
      formData.append('file', file)
      formData.append('type', 'businessLicense')
      const { data: { id, name }} = await uploadLicense(formData)
      this.addForm.businessLicenseUrl = name
      this.addForm.businessLicenseId = id
      this.$refs.ruleForm.validateField('businessLicenseId')
    },
    beforerUpload(file) {
      const isJPG = ['image/jpeg', 'image/png', 'image/jpg'].includes(file.type)
      const isLt5M = file.size / 1024 / 1024 < 5

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt5M) {
        this.$message.error('上传头像图片大小不能超过 5MB!')
      }
      return isJPG && isLt5M
    },
    // name: this.name,
    // legalPerson: this.legalPerson,
    // registeredAddress: this.registeredAddress,
    // industryCode: this.industryCode,
    // contact: this.contact,
    // contactNumber: this.contactNumber,
    // businessLicenseUrl: this.businessLicenseUrl,
    // businessLicenseId: this.businessLicenseId,
    async submitBtn() {
      if (this.$route.params.id) {
        await this.$refs.ruleForm.validate()
        delete this.addForm.businessLicenseName
        delete this.addForm.industryName
        delete this.addForm.rent
        await updateEnterprise({
          ...this.addForm,
          id: this.$route.params.id
        })
        this.$message.success('编辑成功')
      } else {
        await this.$refs.ruleForm.validate()
        console.log(this.addForm)
        await addEnterprise(this.addForm)
        this.$message.success('添加成功')
      }
      this.resetBtn()
      this.$router.back()
    },
    resetBtn() {
      this.$refs.ruleForm.resetFields()
    }
  }
}
</script>

<style scoped lang="scss">
  .add-enterprise {
    background-color: #f4f6f8;
    height: 100vh;

    .add-header {
      display: flex;
      justify-content: space-between;
      padding: 0 20px;
      height: 64px;
      line-height: 64px;
      font-size: 16px;
      font-weight: bold;
      background-color: #fff;

      .left {
        span {
          margin-right: 4px;
        }
        .arrow{
          cursor: pointer;
        }
      }

      .right {
        text-align: right;
      }
    }

    .add-main {
      background: #f4f6f8;
      padding: 20px 130px;

      .form-container {
        background-color: #fff;
        .title {
          height: 60px;
          line-height: 60px;
          padding-left:20px;
          position: relative;
          &::before{
            content: '';
            position: absolute;
            top: 20px;
            left: 0;
            background-color: #506ff6;
            height: 20px;
            width: 3px;
            margin-right: 10px;
          }
        }
        .form {
          margin-bottom: 20px;
          padding: 20px 65px 24px;
          .el-form{
            display: flex;
            flex-wrap: wrap;
            .el-form-item{
              width: 50%;
            }
          }
        }
      }
    }

    .add-footer {
      position: fixed;
      bottom: 0;
      width: 100%;
      padding: 24px 50px;
      color: #000000d9;
      font-size: 14px;
      background: #fff;
      text-align: center;
    }
  }
</style>
